import React, { Component } from 'react';
import { connect } from 'dva';
import { Card, Form, DatePicker, Input, Button } from 'antd';
import { addEditExamine } from 'Services/hu.js';

const STORE_NAME = 'busManageExamine';
const FormItem = Form.Item;
const { TextArea } = Input;

@connect(state => ({
  [STORE_NAME]: state[STORE_NAME],
}))
@Form.create()
export default class examineDetail extends Component {
  constructor(props) {
    super(props);
    window.scope = this;
  }

  getDataFromStore = key => (key ? this.props[STORE_NAME][key] : this.props[STORE_NAME]);

  handleSubmit = e => {
    e.preventDefault();
    const { validateFields } = this.props.form;
    validateFields((err, value) => {
      if (err) return false;
      const {
        detailData: { index, ...params },
      } = this.getDataFromStore();
      params.result = value.result;
      if (value.date1) params.date1 = value.date1.format('YYYY-MM-DD');
      addEditExamine(params).then(() => {
        this.props.history.replace('/publicTransport/examine');
      });
    });
  };

  render() {
    const { detailData: D } = this.getDataFromStore();
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: {
        span: 8,
      },
      wrapperCol: {
        span: 16,
      },
    };

    return (
      <Card title="车辆阶段检查详情">
        <Form style={{ maxWidth: '500px' }} onSubmit={this.handleSubmit}>
          <FormItem label="车牌号" {...formItemLayout}>
            <span>{D.num}</span>
          </FormItem>

          <FormItem label="检查日期" {...formItemLayout}>
            {!D.date1 ? (
              getFieldDecorator('date1')(<DatePicker placeholder="请输入检查时间" />)
            ) : (
              <span>{D.date1}</span>
            )}
          </FormItem>

          <FormItem label="故障情况" {...formItemLayout}>
            {getFieldDecorator('result', {
              initialValue: D.result,
            })(<TextArea rows="3" placeholder="请输入故障情况" />)}
          </FormItem>

          <FormItem wrapperCol={{ offset: 8 }}>
            <Button type="primary" htmlType="submit">
              提交
            </Button>
          </FormItem>
        </Form>
      </Card>
    );
  }
}
